import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;


public class Main {
	
	private static int INT_MAX = 1 << 30;
	

	/**
	 * @param args
	 * @throws IOException 
	 * @throws NumberFormatException 
	 */
	public static void main(String[] args) throws NumberFormatException, IOException {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int testCases = Integer.parseInt(br.readLine().trim());
		while(testCases-- != 0) {
			String str = br.readLine().trim();
			StringTokenizer sToken = new StringTokenizer(str);
			int minOxy = Integer.parseInt(sToken.nextToken());
			int minNit = Integer.parseInt(sToken.nextToken());
			
			
			int n = Integer.parseInt(br.readLine().trim());
			Cylinder[] cylinders = new Cylinder[n];
			for(int i = 0; i < n; i++) {
				str = br.readLine().trim();
				sToken = new StringTokenizer(str);
				cylinders[i] = new Cylinder(Integer.parseInt(sToken.nextToken()), Integer.parseInt(sToken.nextToken()), Integer.parseInt(sToken.nextToken()));
			}
			br.readLine();
			System.out.println(calculateMinWeight(cylinders, minOxy, minNit));
		}
	}
	
	private static int calculateMinWeight(Cylinder[] cylinders, int minQxy, int minNit) {
//		int totalOxy = 0, totalNitrogen = 0;
//		for(int i = 0; i < cylinders.length; i++) {
//			totalOxy += cylinders[i].oxygen;
//			totalNitrogen += cylinders[i].nitrogen;
//		}
		
		int[][] mem1 = new int[minQxy + 1][minNit + 1];
		int[][] mem2 = new int[minQxy + 1][minNit + 1];
		
		
		for(int i = 0; i <= minQxy; i++) {
			for(int j = 0; j <= minNit; j++) {
				mem1[i][j] = INT_MAX;
				mem2[i][j] = INT_MAX;
			}
		}
		mem1[0][0] = 0;
		mem2[0][0] = 0;
		
		for(int k = 0; k < cylinders.length; k++) {
			Cylinder currentCylinder = cylinders[k];
			for(int i = 0; i <= minQxy; i++) {
				for(int j = 0; j <= minNit; j++) {
					int ii = Math.min(i + currentCylinder.oxygen, minQxy);
					int jj = Math.min(j + currentCylinder.nitrogen, minNit);
					
					mem2[ii][jj] = min(mem2[ii][jj], mem1[ii][jj], mem1[i][j] + currentCylinder.weight);
					
					
//					if(i - currentCylinder.oxygen >= 0 && j - currentCylinder.nitrogen >= 0) {
//						mem2[i][j] = Math.min(mem1[i][j], mem1[i - currentCylinder.oxygen][j - currentCylinder.nitrogen] + currentCylinder.weight);
//					} else {
//						mem2[i][j] = INT_MAX;
//					}
				}
			}
			
			int[][] t = mem1;
			mem1 = mem2;
			mem2 = t; 
		}
		
//		int minWeight = INT_MAX;
//		for(int i = minQxy; i >= 0; i--) {
//			minWeight = Math.min(minWeight, mem1[i][minNit]);
//		}
//		
//		for(int j = minNit; j >= 0; j--) {
//			minWeight = Math.min(minWeight, mem1[minQxy][j]);
//		}
		
		return mem1[minQxy][minNit];
	}
	
	private static int min(int x, int y, int z) {
		int t = Math.min(x, y);
		return Math.min(t , z);
	}
	
	private static class Cylinder {
		
		private int oxygen, nitrogen, weight;
		
		private Cylinder(int o, int n, int w) {
			oxygen = o;
			nitrogen = n;
			weight = w;
		}
	}

	
}
